package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {

    @Autowired
    private DeptService deptService;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;


    @RequestMapping(value = "/list",name = "展示用户列表数据")
    public String findAll(@RequestParam(name="page",defaultValue = "1") Integer pageNum,
                         @RequestParam(name="pageSize",defaultValue = "10") Integer pageSize){

        PageInfo<User> pageInfo = userService.findPage(getCompanyId(),pageNum,pageSize);
        request.setAttribute("page",pageInfo); //这个 “page” 不能写错，因为页面就是取的是page中的值
        return "system/user/user-list";
    }

    @RequestMapping(value = "/toAdd",name = "跳转到新增用户的页面")
    public String toAdd( ){
//        查询当前登录人企业的所有部门，放入到request中再跳转页面
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);
        return "system/user/user-add";
    }

    @RequestMapping(value = "/edit",name = "保存用户方法")
    public String save(User user){
//        新增 id上是没有值的
//        修改 id是有值的

        if(StringUtils.isEmpty(user.getId())){ //ID为空 新增
            user.setId(UUID.randomUUID().toString());
            user.setCompanyId(getCompanyId());
            user.setCompanyName(getCompanyName());
            user.setCreateTime(new Date());   // 数据创建时间 为了排序
            userService.save(user);
        }else{
            userService.update(user);
        }
        return "redirect:/system/user/list.do"; //重定向到部门列表中
    }

    @RequestMapping(value = "/toUpdate",name = "进入到用户编辑页面")
    public String toUpdate(String id){
        User user = userService.findById(id);

        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);

        request.setAttribute("user",user);
        return "system/user/user-add";
    }
    @RequestMapping(value = "/delete",name = "删除用户数据")
    public String delete(String id){
        userService.deleteById(id);
        return "redirect:/system/user/list.do";
    }
    @RequestMapping(value = "/roleList",name = "进入到给用户分配角色页面")
    public String roleList(String id){  //id是用户id
//        1、当前用户
        User user = userService.findById(id);
        request.setAttribute("user",user);
//      2、查询当前登录人所在企业的所有的角色
        List<Role> roleList = roleService.findAll(getCompanyId());
        request.setAttribute("roleList",roleList);
//       3、查询用户下的角色id 拼成字符串
//       select role_id from pe_role_user where user_id=?
        List<String> roleIds = roleService.findRoleIdsByUserId(id);
        String userRoleStr = roleIds.stream().collect(Collectors.joining(","));
        request.setAttribute("userRoleStr",userRoleStr);

        return "system/user/user-role";
    }

    @RequestMapping(value = "/changeRole",name = "给用户分配角色方法")
    public String changeRole(String userid,String[] roleIds){  //id是用户id
//      向角色用户中间表中插入数据
        userService.changeRole(userid,roleIds);
        return "redirect:/system/user/list.do"; //重定向到用户列表页面
    }



}
